package org.qf.web;

import org.qf.pojo.SmbmsProvider;
import org.qf.pojo.SmbmsRole;
import org.qf.pojo.SmbmsUser;
import org.qf.service.SmbmsProviderService;
import org.qf.service.SmbmsRoleService;
import org.qf.service.SmbmsUserService;
import org.qf.service.impl.SmbmsProviderServiceImpl;
import org.qf.service.impl.SmbmsRoleServiceImpl;
import org.qf.service.impl.SmbmsUserServiceImpl;
import org.qf.utils.Constants;
import org.qf.utils.PageSupport;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author young
 * @create 2021-01-14-14:10
 * @description
 */
@WebServlet(value = "/ProviderServlet")
public class SmbmsProviderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //调用业务层实现
        SmbmsProviderService smbmsProviderService=new SmbmsProviderServiceImpl();
        String proName=req.getParameter("proName");
        if(proName==null)
        {
            proName="";
        }
        //获取当前页
        String pageIndex=req.getParameter("pageIndex");
        int currentPage=1;
        if(pageIndex !=null)
        {
            currentPage=Integer.parseInt(pageIndex);
        }
        int pageSize=Constants.pageSize;
        int smbmsProbiderCount=0;

        try {
            smbmsProbiderCount=smbmsProviderService.getSmbmsProviderCount(proName);
            PageSupport pageSupport=new PageSupport();
            pageSupport.setCurrentPageNo(currentPage);
            pageSupport.setPageSize(pageSize);
            pageSupport.setTotalCount(smbmsProbiderCount);
            int totalPageCount = pageSupport.getTotalPageCount();
            if(currentPage<1)
            {
                currentPage=1;
            }else if(currentPage>totalPageCount)
            {
                currentPage=totalPageCount;
            }
            List<SmbmsProvider> smbmsProviders=smbmsProviderService.queryList(proName,currentPage,pageSize);
            if(smbmsProviders.size()>0)
            {
                req.setAttribute("pages",pageSupport);
                req.setAttribute("proName",proName);
                req.setAttribute("list",smbmsProviders);
            }
            req.getRequestDispatcher("providerList.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
